package com.e2edour.service.actor

import com.alibaba.fastjson.{JSON, JSONObject}
import com.e2edour.runtime.properties.AppContextConfig
import com.e2edour.runtime.utils.HttpUtil
import com.e2edour.service.bo.{TuringResBean}
import org.slf4j.{Logger, LoggerFactory}

/**
  * <p>
  * 发送图灵接口
  *
  * @author King 
  * @since 2017/1/18
  */
object TuringActor {

  private val logger: Logger = LoggerFactory.getLogger(getClass)

  def send(msg: String, userid: String): TuringResBean = {

    val json = new JSONObject();
    //env 文件中配置图灵的apikey
    json.put("key", AppContextConfig.get("turing.apiKey"));
    json.put("info", msg);
    json.put("userid", userid)

    logger.info(s"===>[$userid] ask :[$msg]")
    //模式匹配
    msg match {
      case _ =>
        //图灵开放api调用地址http://www.tuling123.com/openapi/api
        val result = HttpUtil.sendPost(AppContextConfig.get("turing.url"), json.toString())
        logger.info(s"===>图灵api返回:$result")
        val obj: TuringResBean = JSON.parseObject(result, classOf[TuringResBean])
        obj
    }
  }
}
